<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('客户类型')" prop="investType">
            <dict-select
              v-model="form.investType"
              dictionary="lp_type"
              disabled
              :placeholder="$t('客户类型')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户名称')" prop="investId">
            <el-select v-model="form.investId" :placeholder="$t('客户名称')" disabled clearable>
              <el-option
                v-for="dict in lpBaseOption"
                :key="dict.name"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('问卷类型')" prop="type">
            <dict-select
              v-model="form.type"
              dictionary="questionnaire_type"
              :placeholder="$t('问卷类型')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('问卷名称')" prop="questionNameId">
            <el-select v-model="form.questionNameId" :placeholder="$t('问卷名称')" clearable>
              <el-option
                v-for="dict in lpQuestionnaireNameOption"
                :key="dict.questionnaireName"
                :label="dict.questionnaireName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总分')" prop="score">
            <el-input-number v-model="form.score" :placeholder="$t('总分')" show-word-limit maxlength="10" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('答题时间')" prop="time">
            <el-date-picker
              v-model="form.time"
              clearable
              disabled
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('答题时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('对应结果')" prop="result">
            <el-select v-model="form.result" :placeholder="$t('对应结果')" clearable disabled>
              <el-option
                v-for="dict in resultOption"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="lp_questionnaire_investigation" :btn-show="!otherBtnHide" />
    </el-form>
    <div v-if="editStatus" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="lp:investigation" />
    </div>
  </div>
</template>

<script>
import {
  saveLpQuestionnaireInvestigation,
  getLpQuestionnaireInvestigation
} from '@/api/fund/questionnaire/investigation/lpQuestionnaireInvestigation'
import { getLpBaseByType } from '@/api/fund/questionnaire/settings/questionnaireQuestion/lpQuestionnaireQuestion'
import { listLpQuestionnaireSet } from '@/api/fund/questionnaire/settings/questionnaireSet/lpQuestionnaireSet'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      otherBtnHide: false,
      // 表单校验
      rules: {
        investType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        investId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        type: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        name: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      lpBaseOption: [],
      lpQuestionnaireNameOption: [],
      editStatus: true,
      resultOption: []
    }
  },
  watch: {
    'form.investType': {
      handler(val) {
        getLpBaseByType(val).then(response => {
          this.lpBaseOption = response.data
        })
      }
    }
  },
  created() {
    this.getDicts('lp_type').then(response => {
      this.investTypeOptions = response.data
    })
    // 获取问卷名称
    listLpQuestionnaireSet().then(response => {
      this.lpQuestionnaireNameOption = response.rows
    })
    this.getDicts('corresponding_results').then(response => {
      this.resultOption = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data, editStatus) {
      this.loading = true
      this.editStatus = editStatus === undefined ? true : editStatus
      this.otherBtnHide = !this.editStatus
      if (data != null && data.id !== undefined) {
        getLpQuestionnaireInvestigation(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dataId: undefined,
        investType: undefined,
        investId: undefined,
        type: undefined,
        name: undefined,
        time: undefined,
        score: undefined,
        result: undefined,
        status: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveLpQuestionnaireInvestigation(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
